/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.groupbuy.api.listener;

import com.yami.shop.bean.app.dto.SkuDto;
import com.yami.shop.bean.app.vo.GroupActivityVO;
import com.yami.shop.bean.app.vo.GroupSkuVO;
import com.yami.shop.bean.enums.ProdType;
import com.yami.shop.bean.event.LoadProdActivistEvent;
import com.yami.shop.bean.order.LoadProdActivistOrder;
import com.yami.shop.groupbuy.common.api.dto.ApiGroupActivityDto;
import com.yami.shop.groupbuy.common.api.dto.ApiJoinGroupTeamDto;
import com.yami.shop.groupbuy.common.enums.GroupActivityStatusEnum;
import com.yami.shop.groupbuy.common.enums.TeamStatusEnum;
import com.yami.shop.groupbuy.common.model.GroupTeam;
import com.yami.shop.groupbuy.common.service.GroupActivityService;
import com.yami.shop.groupbuy.common.service.GroupTeamService;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 加载商品团购信息
 * @author yami
 */
@Component("groupProdActivistListener")
@AllArgsConstructor
public class LoadProdActivistListener {

    private GroupActivityService groupActivityService;

    private GroupTeamService groupTeamService;

    private MapperFacade mapperFacade;

    @EventListener(LoadProdActivistEvent.class)
    @Order(LoadProdActivistOrder.DEFAULT)
    public void loadProdGroupHandle(LoadProdActivistEvent event) {
        // 不是团购商品，不用处理
        if (!Objects.equals(event.getProdType(), ProdType.PROD_TYPE_GROUP.value())) {
            return;
        }

        // 查询拼团活动信息
        ApiGroupActivityDto apiGroupActivityDto = groupActivityService.getByProdId(event.getProdId());
        GroupActivityVO groupActivityVO = mapperFacade.map(apiGroupActivityDto, GroupActivityVO.class);
        long currentTimeMillis = System.currentTimeMillis();

        // 无法获取团购信息、团购没有启用、当前时间大于活动结束时间， 则不添加活动信息
        boolean unActivity = Objects.isNull(apiGroupActivityDto)  ||
                !Objects.equals(apiGroupActivityDto.getStatus(), GroupActivityStatusEnum.ENABLE.value()) ||
                currentTimeMillis > groupActivityVO.getEndTime().getTime();
        // 没有开启预热且开始时间未到
        if (unActivity || Objects.equals(apiGroupActivityDto.getIsPreheat(),0) && currentTimeMillis < groupActivityVO.getStartTime().getTime()) {
            return;
        }
        // 插入sku信息
        Map<Long, SkuDto> skuMap = event.getProductVO().getSkuList().stream().collect(Collectors.toMap(SkuDto::getSkuId, s -> s));
        Iterator<GroupSkuVO> iterator = groupActivityVO.getGroupSkuList().iterator();
        while (iterator.hasNext()) {
            GroupSkuVO groupSkuVO = iterator.next();
            // 启用的sku中没有包含该团购sku，则删除该sku
            if (!skuMap.containsKey(groupSkuVO.getSkuId())) {
                iterator.remove();
                continue;
            }
            SkuDto skuDto = skuMap.get(groupSkuVO.getSkuId());
            groupSkuVO.setStocks(skuDto.getStocks());
            groupSkuVO.setPic(skuDto.getPic());
            groupSkuVO.setPrice(skuDto.getPrice());
            groupSkuVO.setProperties(skuDto.getProperties());
            groupSkuVO.setSkuName(skuDto.getSkuName());
        }

        // 获取拼团队伍
        ApiJoinGroupTeamDto joinTeam = groupTeamService.getJoinGroupTeamHasMaxNum(groupActivityVO.getGroupActivityId());
        if (joinTeam != null) {
            GroupTeam groupTeam = groupTeamService.getById(joinTeam.getJoinGroupTeamId());
            if (Objects.equals(groupTeam.getStatus(), TeamStatusEnum.IN_GROUP.value())) {
                groupActivityVO.setJoinGroupTeamId(joinTeam.getJoinGroupTeamId());
            }
            groupActivityVO.setProdCount(joinTeam.getProdCount());
        }

        // 获取商品活动最低价
        event.getProductVO().setGroupActivityVO(groupActivityVO);
    }

}
